﻿'Phung Huu Tu
'--------------------------------------------------------------------------------------
' Application name: Quicklaucher
' This application using for quicky access all application in computer your computer
' If you find a bug please contact me hu_tu@brycen.com.vn
' Thanks
'--------------------------------------------------------------------------------------

Imports System.IO

Public Class MainForm
    'Default path to search
    Dim Files As FileInfo()
    Dim Folder As DirectoryInfo
    Const Path As String = "C:\ProgramData\Microsoft\Windows\Start Menu\"
    Dim Hotkey, tmp, sFoundText As String
    Public Shared pa, code, flg, control, key As String

    'Hide form when deactive
    Private Sub MainForm_Deactivate(sender As Object, e As EventArgs) Handles Me.Deactivate
        Me.Hide()
    End Sub

    'Unhook
    Private Sub _FormClosing(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles Me.FormClosing
        KeyboardHook.RemoveHook()
    End Sub

    'SetHook
    Private Sub _Load(sender As Object, e As EventArgs) Handles MyBase.Load

        'initial hook and files array
        KeyboardHook.SetHook()

        'Trayicon visiable
        QuickTray.Visible = True

        'Load program file search
        Folder = New DirectoryInfo(Path)
        Files = Folder.GetFiles("*", SearchOption.AllDirectories)

        'Set Alt Space is default key
        Hotkey = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\QuickLaucher", "Hotkey", "")

        If Hotkey = "" Then
            Hotkey = "32-32-260-Alt-Space"
            Startup.AddHotkey(Hotkey)
            Prehotkey(Hotkey)
        Else
            Prehotkey(Hotkey)
        End If
    End Sub

    'Share hotkey
    Public Shared Sub Prehotkey(Hotkey As String)
        Dim temp As String() = Split(Hotkey, "-")
        code = temp(0)
        flg = temp(1)
        pa = temp(2)
        control = temp(3)
        key = temp(4)
    End Sub

    'Full fill file name
    Private Sub KeyBox_Leave(sender As Object, e As EventArgs) Handles KeyBox.Leave
        'AutoCompleteCombo_Leave(KeyBox)
    End Sub

    'Autocompleted combox from Codeprojet
    Public Sub AutoCompleteCombo_KeyUp(ByVal cbo As ComboBox, ByVal e As KeyEventArgs)
        Dim sTypedText As String
        Dim iFoundIndex As Integer
        Dim oFoundItem As Object
        'Dim sFoundText As String
        'Dim sAppendText As String

        'Allow select keys without Autocompleting
        Select Case e.KeyCode
            Case Keys.Back, Keys.Left, Keys.Right, Keys.Up, Keys.Delete, Keys.Down, Keys.Home, Keys.ShiftKey, Keys.LControlKey
                Return
        End Select

        'Get the Typed Text and Find it in the list
        sTypedText = Keytxt.Text
        'iFoundIndex = cbo.FindString(sTypedText)
        cbo.Items.Clear()
        'Add item
        For Each File In Files
            tmp = File.Name.ToLower
            If tmp.Contains(sTypedText) Then
                cbo.Items.Add(tmp)
            End If
        Next

        'If we found the Typed Text in the list then Autocomplete
        If sTypedText.Trim <> "" AndAlso cbo.Items.Count >= 0 Then
            'If sTypedText.Trim <> "" AndAlso iFoundIndex >= 0 Then

            'Get the Item from the list (Return Type depends if Datasource was bound or List Created)
            iFoundIndex = cbo.FindString(sTypedText)

            If iFoundIndex = -1 Then
                Return
            Else
                oFoundItem = cbo.Items(iFoundIndex)

                'Use the ListControl.GetItemText to resolve the Name in case the Combo was Data bound
                sFoundText = cbo.GetItemText(oFoundItem)

                ''Append then found text to the typed text to preserve case
                'sAppendText = sFoundText.Substring(sTypedText.Length)
                'cbo.Text = sTypedText & sAppendText

                ''Select the Appended Text
                'cbo.SelectionStart = sTypedText.Length
                'cbo.SelectionLength = sAppendText.Length

                'Dropped down menu
                If cbo.Items.Count > 0 Then
                    cbo.DroppedDown = True
                End If
            End If
        Else
            cbo.DroppedDown = False
        End If
    End Sub

    'Set value when notfocus combobox
    Public Sub AutoCompleteCombo_Leave(ByVal cbo As ComboBox)
        Dim iFoundIndex As Integer

        iFoundIndex = cbo.FindString(cbo.Text)

        cbo.SelectedIndex = iFoundIndex

    End Sub

    'Preprocess enter key and keypress on combobox
    Private Sub KeyBox_KeyUp(sender As Object, e As KeyEventArgs) Handles KeyBox.KeyUp
        'event enter key on combobox
        If KeyBox.SelectedText.Trim <> "" AndAlso e.KeyCode = Keys.Enter Then
            For Each File In Files
                If File.Name.ToLower.Contains(sFoundText) Then
                    Process.Start(File.FullName)
                End If
            Next
        Else
            'next recive key on combobox
            AutoCompleteCombo_KeyUp(KeyBox, e)
        End If

    End Sub

    'Show option form
    Private Sub btnOption_Click(sender As Object, e As EventArgs) Handles btnOption.Click
        OptionForm.Show()
    End Sub

    'Double click on tray
    Private Sub QuickTray_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles QuickTray.MouseDoubleClick
        Me.Show()
        Me.Activate()
    End Sub

    'Left and right mouse click on tray
    Private Sub QuickTray_MouseClick(sender As Object, e As MouseEventArgs) Handles QuickTray.MouseClick
        If e.Button = Windows.Forms.MouseButtons.Right Then
            ContextMenuStrip1.Show()
        End If
    End Sub

    'Show balloon tip on traybar
    Private Sub MainForm_Move(sender As Object, e As EventArgs) Handles Me.Move
        If (Me.Visible = False) Then
            QuickTray.ShowBalloonTip(3000, "QuickLaucher", "Press hotkey to show/hide window", ToolTipIcon.Info)
        End If
    End Sub

    'Show mainform
    Private Sub QuickLauncherToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles QuickLauncherToolStripMenuItem.Click
        Me.Show()
        Me.Activate()
    End Sub

    'Show optionform
    Private Sub OptionsToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles OptionsToolStripMenuItem.Click
        OptionForm.Show()
        OptionForm.Activate()
    End Sub

    'Show aboutform
    Private Sub AboutToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles AboutToolStripMenuItem.Click
        AboutForm.Show()
        AboutForm.Activate()
    End Sub

    'Exit application
    Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
        Application.Exit()
    End Sub

    'Search box
    Private Sub Keytxt_KeyUp(sender As Object, e As KeyEventArgs) Handles Keytxt.KeyUp
        If Keytxt.Text.Trim <> "" AndAlso e.KeyCode = Keys.Enter Then
            For Each File In Files
                If File.Name.ToLower.Contains(sFoundText) Then
                    Process.Start(File.FullName)
                End If
            Next
        Else
            AutoCompleteCombo_KeyUp(KeyBox, e)
        End If
    End Sub
End Class
